package com.example.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.example.response.ErrorResult;
import com.example.utils.CheckUtil;
import com.example.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Slf4j
@Component
public class Interceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        TokenNotValidation annotation = ((HandlerMethod) handler).getMethodAnnotation(TokenNotValidation.class);
        if (annotation != null) {
            return true;
        }
        //如果不是映射到方法直接通过
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        String path = request.getServletPath();
        System.out.println(path);
        path = CheckUtil.getPath(path);
        String token = request.getHeader("Authorization");
        if (token == null) {
            returnJson(response, "请登录");
            return false;
        }
        String dd = JwtUtil.getRole(token.substring(7));
        System.out.println(dd);
        if (JwtUtil.getRole(token.substring(7)).equals("admin")) {
            return true;
        }
        String check = null;
        if (token != null) {
            List<String> auth = JwtUtil.getAuth(token.substring(7));
            for (String s : auth) {
                if (CheckUtil.getPathWithSplit(path).equals(s)) {
                    check = "1";
                }
            }
        }
        if (token != null) {
            if (JwtUtil.verify(token.substring(7))) {
                if (check == "1") {
                    return true;
                } else {
                    returnJson(response, "您没有这个接口的权限");
                    log.info("您没有这个接口的权限");
                    return false;
                }
            } else {
                returnJson(response, "最后拦截请确认错误");
                log.info("最后拦截请确认错误");
                return false;
            }
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

    private void returnJson(HttpServletResponse response, String returnmsg) {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try {
            writer = response.getWriter();
            ErrorResult fail = ErrorResult.fail(401, returnmsg);
            String besnString = JSONObject.toJSONString(fail);
            writer.print(besnString);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
}
